<style>
#UserPage{
	padding: 12px;
	border-radius: 2px;
	background: #AAA;
	display: inline-block;
}
.LoginTable{
	display: inline-block;
}
.LoginTable input{
	padding: 8px 4px;
	margin-top: 20px;
}
#LoginButton, #MailLoginButton{
	width: 100%;
	margin-bottom: 24px;
	box-sizing: border-box;
}
#ResetPasswdLink{
	float: right;
}
#SendMailButton{
	float: right;
	background: none;
	box-shadow: none;
	padding-right: 1px;
	border: 1px solid rgba(0,0,0,0);
}
#SendMailButton:hover{
	color: #982;
}
#SendMailButton:active{
	color: #661;
}
#UserLoginLabel, #MailLoginLabel{
	padding: 8px;
	color: #333;
	cursor: default;
	font-size: 1.5rem;
	font-weight: bold;
	text-align: center;
	border-bottom: 4px solid #EEE;
}
#RegisterLink, #ResetPasswdLink, #AgreementLink{
	text-decoration: none;
}
</style>

<div id='UserPage'>
	<table>
		<tr><td id='MailLoginLabel'>免密登录</td><td id='UserLoginLabel'>密码登录</td></tr>
		<tr><td colspan='2' id='LoginPage'>
			<table class='LoginTable'>
				<tr><td><v-text title='用户名称' id='LoginUserText' maxlength='32' size='28' placeholder='用户名或邮箱'></v-text></td></tr>
				<tr><td><v-text title='登录密码' id='LoginPasswdText' maxlength='32' type='password' size='28' placeholder='用户登录密码'></v-text></td></tr>
				<tr><td><v-button title='立即登录' id='LoginButton'></v-button></td></tr>
				<tr><td>
						<span class='RegisterLabel'>还没有账号？</span>
						<a id='RegisterLink' href='javascript:void(0)'>立即注册<a>
						<a id='ResetPasswdLink' href='javascript:void(0)'>重置密码<a>
				</td></tr>
			</table>
		</td></tr>
		<tr><td colspan='2' id='RegisterPage'>
			<table class='LoginTable'>
				<tr><td><v-text title='用户邮箱' id='UserMailText' maxlength='32' size='28' placeholder='邮箱地址'></v-text></td></tr>
				<tr><td><v-text title='验证密码' id='CheckCodeText' maxlength='6' size='10' placeholder='验证码'></v-text><v-button id='SendMailButton' title=' 发送验证码 '></v-button></td></tr>
				<tr><td><v-button title='立即登录' id='MailLoginButton'></v-button></td></tr>
				<tr><td><span class='RegisterLabel'>阅读并同意<a id='AgreementLink' target='_blank' href='/sharenote?title=AGREEMENT'>《寻根用户协议》</a>&nbsp;自动注册账号</span></td></tr>
			</table>
		</td></tr>
	</table>
</div>

<script>
var curmail = null;

getVue('UserPage');

$('#LoginUserText').keydown(function(e){
	if (e.which == 13) $('#LoginPasswdText').focus();
});

$('#LoginPasswdText').keydown(function(e){
	if (e.which == 13) $('#LoginButton').click();
});

$('#UserMailText').keydown(function(e){
	if (e.which == 13) $('#CheckCodeText').focus();
});

$('#CheckCodeText').keydown(function(e){
	if (e.which == 13) $('#MailLoginButton').click();
});

$('#MailLoginLabel').click(function(){
	$('#LoginPage table').hide();
	$('#RegisterPage table').show();
	$(this).css('border-bottom', '4px solid #982').css('color', '#982');
	$('#UserLoginLabel').css('border-bottom', '4px solid #EEE').css('color', '#666');

	var mail = getStorage('mail');

	if (strlen(mail) > 0) $('#UserMailText').val(mail);
});

$('#UserLoginLabel').click(function(){
	$('#LoginPage table').show();
	$('#RegisterPage table').hide();
	$(this).css('border-bottom', '4px solid #982').css('color', '#982');
	$('#MailLoginLabel').css('border-bottom', '4px solid #EEE').css('color', '#666');

	var user = getStorage('user');

	if (strlen(user) > 0) $('#LoginUserText').val(user);
});

function hideRegist(){
	$('#UserLoginLabel').click();
	$('#MailLoginLabel').remove();
	$('.RegisterLabel').parent().hide();
}

function checkEmail(mail){
　　var reg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
　　return reg.test(mail);
}

$('.LoginTable input').focus(function(){
	setToastErrorText('');
});

$('#SendMailButton').click(function(){
	var mail = $('#UserMailText').val();
	
	if (strlen(mail) == 0){
		$('#UserMailText').focus();
	}
	else if (checkEmail(mail)){
		getHttpResult("/SendMail", {flag: 'L', mail: mail}, function(data){
			if (data.code >= 0){
				var sec = 60;
				var txt = $('#UserMailText');
				var btn = $('#SendMailButton');
				
				$('#CheckCodeText').val('');
				txt.attr('disabled', true);
				btn.attr('disabled', true);
				btn.val(sec + '秒后可重试');
				curmail = mail;
				
				var timer = setInterval(function(){
					if (--sec <= 0){
						txt.removeAttr('disabled');
						btn.removeAttr('disabled');
						btn.val('发送验证码');
						clearInterval(timer);
					}
					else{
						txt.attr('disabled', true);
						btn.attr('disabled', true);
						btn.val(sec + '秒后可重试');
					}
					
				}, 1000);
			}
			else if (data.code == XG_DUPLICATE){
				setToastErrorText('该邮箱已经注册');
			}
			else{
				showMessage("<div style='padding:8px 12px'>发送频繁，请稍后再试！</div>", "系统提示");
			}
		});
	}
	else{
		setToastErrorText('邮箱地址格式错误');
	}
});

$('#MailLoginButton').click(function(){
	var mail = $('#UserMailText').val();
	var code = $('#CheckCodeText').val();
	
	if (strlen(mail) == 0){
		$('#UserMailText').focus();
	}
	else if (strlen(code) == 0){
		$('#CheckCodeText').focus();
	}
	else{
		getHttpResult('/checklogin', {mail: mail, code: code}, function(data){
			if (data.code == XG_AUTHFAIL || data.code == XG_TIMEOUT){
				setToastErrorText('请重新发送验证码');
			}
			else if (data.code == XG_PARAMERR){
				setToastErrorText('请输入正确的验证码');
			}
			else if (data.code == XG_NOTFOUND){
				setToastErrorText('用户不存在或已停用');
			}
			else if (data.code < 0){
				setToastErrorText('用户登录失败');
			}
			else{
				setStorage('mail', mail);
				setCurrentUser(data.user, true);
			}
		});
	}
});

$('#LoginButton').click(function(){
	var user = $('#LoginUserText').val();
	var passwd = $('#LoginPasswdText').val();
	
	if (strlen(user) == 0){
		$('#LoginUserText').focus();
	}
	else if (strlen(passwd) == 0){
		$('#LoginPasswdText').focus();
	}
	else{
		clearCookie();

		getHttpResult('/checklogin', {user: user, passwd: passwd}, function(data){
			if (data.code == XG_AUTHFAIL){
				if (data.times <= 0){
					setToastErrorText('登录频繁(请使用验证码登录)');
					$('#UserMailText').val('');
					$('#CheckCodeText').val('');
					$('#MailLoginLabel').click();
				}
				else if (data.times <= 1){
					showMessage('你还有最后' + data.times + '次登录机会！', '登录频繁');
				}
				else if (data.times <= 3){
					setToastErrorText('你还有' + data.times + '次登录机会');
				}
				else{
					setToastErrorText('用户身份认证失败');
				}
			}
			else if (data.code == XG_NOTFOUND){
				setToastErrorText('用户不存在或已停用');
			}
			else if (data.code < 0){
				if (data.timeout && data.timeout > 0){
					setToastErrorText('登录频繁(锁定账号' + data.timeout / 60 + '分钟)');
				}
				else{
					setToastErrorText('用户登录失败');
				}
			}
			else{
				setCurrentUser(data.user, true);
			}
		});
	}
});

$('#RegisterLink').click(function(){
	var url = '/app/workspace/pub/register.htm';
	var icon = '/res/img/menu/user.png';

	window.location.href = '/singlepage?path=' + encodeURIComponent(url) + '&icon=' + encodeURIComponent(icon) + '&title=用户注册';
});

$('#ResetPasswdLink').click(function(){
	var url = '/app/workspace/pub/password.htm';
	var icon = '/res/img/menu/user.png';

	window.location.href = '/singlepage?path=' + encodeURIComponent(url) + '&icon=' + encodeURIComponent(icon) + '&title=密码重置';
});

getHttpResult('/sendmail', {flag: 'C'}, function(data){
	if (data.code > 0){
		$('#MailLoginLabel').click();
	}
	else{
		hideRegist();
	}
});

</script>